package com.cnhis.cloudhealth.clinical.hosdoctor.inspectreport.bo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosdoctor.doctorparameter.bo.DoctorparameterBo;
import com.cnhis.cloudhealth.clinical.hosdoctor.inspectreport.dao.InspectreportDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

/**
 * Created by xiezhenyuan on 2016/3/24.
 */
@Service
public class InspectreportBo {
    @Resource
    private InspectreportDao inspectreportDao;
    @Autowired
    private DoctorparameterBo doctorparameterBo;

    private ModelVo packModelVoPage(String nid,Map map,int pageno,int pagesize) {
        ModelVo mo = new ModelVo();
        try {
            Page p = inspectreportDao.queryForPage(nid, map, pageno, pagesize);
            if (p.getList() != null && p.getList().size() > 0) {
                mo.setList(p.getList());
                mo.setResult(StaticKeys.OH_SUCCESS);
                mo.setTotal(p.getTotalNumber());
                mo.setRows(p.getList());
                mo.setPage(pageno);
                mo.setRecords(p.getTotalNumber());
                mo.setTotalPage(p.getTotalPage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mo;
    }

    private ModelVo packModelVo(String nid,Map map){
        ModelVo mo = new ModelVo();
        List<Map<String,String>> list = null;
        try {
            list = inspectreportDao.queryForList(nid,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(list!=null&&list.size()>0) {

            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setTotal(list.size());
        }
        return mo;
    }


    public ModelVo selectExamreport(Map map,int pageno,int pagesize){return packModelVoPage("Inpectreport.7304",map,pageno,pagesize);}
    public ModelVo selectDefaultinstrument(){return packModelVo("Inpectreport.4236",null);}
    public ModelVo selectExamresult(Map map){return packModelVo("Inpectreport.7305",map);}
    public ModelVo selectExamimage(Map map){return packModelVo("Inpectreport.7306",map);}
    public Map selectExamimage2(Map map)throws Exception{
        Map eimage = (Map)inspectreportDao.queryToObject("Inpectreport.7306_2",map);
        return eimage;
    }
    public ModelVo selectDoctorExamresult(Map map, int currentPage, int pageSize){
        return packModelVoPage("Inpectreport.7251",map,currentPage,pageSize);
    }
    public ModelVo selectImagerepsult(Map map){return packModelVo("Inpectreport.11309",map);}
    public ModelVo selectImagerepimg(Map map){return packModelVo("Inpectreport.11296",map);}
    public ModelVo selectPrintrepcon(Map map){return packModelVo("Inpectreport.11299",map);}
    public ModelVo selectPrintrepimg(Map map){return packModelVo("Inpectreport.11300",map);}

    public ModelVo selectRepimpro(Map map){
        ModelVo mo = new ModelVo();
        try {
            List list = inspectreportDao.queryForList("Inpectreport.11328_001",map);
            if(list!=null&&list.size()>0){
                mo = packModelVo("Inpectreport.11328_003",map);
            }else{
                List<?> list2 = inspectreportDao.queryForList("Inpectreport.11328_002",map);
                if(list2!=null&&list2.size()>0){
                    for(Object o:list2){
                        if(o instanceof Map){
                            Map<String,Object> m = (Map<String,Object>)o;
                            Integer paa18 = (Integer)m.get("paa18");
                            Integer ABW01 = (Integer)m.get("ABW01");
                            Integer AAU01 = (Integer)m.get("AAU01");
                            map.put("paa18",paa18);
                            map.put("ABW01",ABW01);
                            map.put("AAU01",AAU01);
                           mo = packModelVo("Inpectreport.11328_004",map);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mo;
    }
    public ModelVo selectUnsureplist(Map map){return packModelVo("Inpectreport.7267",map);}
    public ModelVo selectCheckcrisis(Map map,int pageno,int pagesize){
        ModelVo mo = new ModelVo();
        Integer type =  MapUtils.getInteger(map,"type");
        try {
            String sysval =  (String)inspectreportDao.queryToObject("Inpectreport.7744_001",null);
            if(sysval!=null&&!"1".equals(sysval)){

                if(type.intValue()==0){
                    mo = packModelVoPage("Inpectreport.7744_002",map,pageno,pagesize);
                }else{
                    mo = packModelVoPage("Inpectreport.7744_003",map,pageno,pagesize);
                }
            }else{
                if(type.intValue()==0){
                    mo = packModelVoPage("Inpectreport.7744_004",map,pageno,pagesize);
                }else{
                    mo = packModelVoPage("Inpectreport.7744_005",map,pageno,pagesize);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mo;
    }
    public ModelVo selectEmrblremind(Map map){return packModelVo("Inpectreport.1719",map);}

    @SuppressWarnings({"unchecked", "rawtypes"})
    public ModelVo doctorViewImageResult(Map map, int currentPage, int pageSize)
            throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            Map hashMap = new HashMap();
            hashMap.put("BLP02", 58);
            List<Map<String,Object>> list = (List<Map<String,Object>>)inspectreportDao.queryForExtObject("Inpectreport.7301_000",map);
            String value = doctorparameterBo.getSysValue(300,301004,140);
            String value2 = doctorparameterBo.getSysValue(300,301004,141);
            String value3 = doctorparameterBo.getSysValue(300,301004,142);
            String value4 = doctorparameterBo.getSysValue(300,301004,143);
            map.put("fhost", value);
            map.put("fport", value2);
            map.put("fuser", value3);
            map.put("fpass", value4);
            if (list != null && list.size() > 0){
                pg = inspectreportDao.queryForPage("Inpectreport.7301_002", map, currentPage, pageSize);
            }

            else{
                pg = inspectreportDao.queryForPage("Inpectreport.7301_001",map, currentPage, pageSize);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }

        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
}
